<template>
  <div>
    <div style="margin-bottom: 10px; width: 100%;align-items: center; display: flex; align-items: center; justify-content: center;">
      <el-card style="width: 60%;
        border-radius: 30px;
        align-items: center;
        display: flex; align-items: center;
        justify-content: center;
        background: linear-gradient(135deg, #98efb1, #99bbf6);
        color: white; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);">
        <strong style="font-size: 24px; font-family: '楷体'">系统首页欢迎-{{ user.name }}</strong>
      </el-card>
    </div>

    <div>
      <el-row :gutter="10">

        <el-col :span="12">
          <el-card>
            <div style="width: 100%; height: 380px" id="line">

            </div>
          </el-card>

        </el-col>

        <el-col :span="12">
          <el-card>
            <div style="width: 100%; height: 380px" id="bar">
            </div>
          </el-card>
        </el-col>

      </el-row>
    </div>
    <div style="margin-top: 20px">
      <el-row :gutter="10">

        <el-col :span="12">
          <el-card>
            <div style="width: 100%; height: 380px" id="pie1">

            </div>
          </el-card>

        </el-col>

        <el-col :span="12">
          <el-card>
            <div>系统公告</div>
            <div style="width: 100%; height: 360px">
              <el-collapse v-model="activeName" accordion style="padding: 0">
                <el-collapse-item v-for="(item, index) in Info" :key="index" :name="index">
                  <template slot="title">
                    <i class="el-icon-tickets"></i>
                    {{ item.title }}
                  </template>
                  <div style="align-items: center;display: flex; align-items: center; justify-content: center;">创建时间: {{ item.time }}</div><br>
                  <div>{{ item.content }}</div>
                </el-collapse-item>
              </el-collapse>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts';
import request from "@/utils/request"

const option = {
  title: {
    text: '订单金额统计图',
    left: 'center'
  },
  legend: {
    top: '5%',
    left: 'left'
  },
  tooltip: {
    trigger: 'axis'
  },
  xAxis: {
    type: 'category',
    data: []
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      name: '订单金额',
      data: [],
      type: 'line',
      smooth: true
    }
  ]
};

const option1 = {
  title: {
    text: '订单种类柱状图',
    left: 'center'
  },
  tooltip: {
    trigger: 'axis'
  },
  legend: {
    top: '5%',
    left: 'left'
  },
  xAxis: {
    type: 'category',
    data: ['水果', '蔬菜', '饮料', '零食', '日常用品', '灯具', '家电']
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      name: '订单金额',
      data: [],
      type: 'bar',
      smooth: true
    }
  ]
};

const option3 = {
  title: {
    text: '订单金额统计饼状图',
    subtext: 'Fake Data',
    left: 'center'
  },
  tooltip: {
    trigger: 'item'
  },
  legend: {
    orient: 'vertical',
    top: '5%',
    left: 'left'
  },
  series: [
    {
      name: 'Access From',
      type: 'pie',
      radius: ['40%', '70%'],
      avoidLabelOverlap: false,
      padAngle: 5,
      itemStyle: {
        borderRadius: 10
      },
      label: {
        show: true,
        position: 'center',
        formatter(param) {
          return param.name + '(' + param.percent + '%)';
        }
      },
      emphasis: {
        label: {
          show: true,
          fontSize: 40,
          fontWeight: 'bold'
        }
      },
      labelLine: {
        show: false
      },
      data: []
    }
  ]
};

export default {
  name: "Charts",
  data() {
    return {
      user: JSON.parse(localStorage.getItem('user11') || '{}'),
      Info: [],
      activeName: '0'
    }
  },
  methods: {
    loadNotice() {
      request.get('/notice/list').then(res => {
        this.Info = res.data
      })
    }
  },
  created() {
    this.loadNotice()
  },
  mounted() {
    let linetDom = document.getElementById('line');
    let lineChart = echarts.init(linetDom);
    lineChart.setOption(option);

    let barDom = document.getElementById('bar');
    let barChart = echarts.init(barDom);
    barChart.setOption(option1);

    let pie1Dom = document.getElementById('pie1');
    let pie1Chart = echarts.init(pie1Dom);
    pie1Chart.setOption(option3)

    request.get("/login/charts").then(res => {
          if (res.code === 200) {
            let linexAxis = res.data.line.map(item => item.date)
            let lineseries = res.data.line.map(item => item.value)
            option.xAxis.data = linexAxis
            option.series[0].data = lineseries
            lineChart.setOption(option)

            let barxAxis = res.data.bar.map(item => item.category)
            let barseries = res.data.bar.map(item => item.value)
            option1.xAxis.data = barxAxis
            option1.series[0].data = barseries
            barChart.setOption(option1)

            let pieseries1 = res.data.pie
            option3.series[0].data = pieseries1
            pie1Chart.setOption(option3)
          }
        },
    )
  }
}
</script>

<style scoped>
body > .el-container {
  margin-bottom: 40px;
}

.qqq {
  background-color: #000c17 !important;
}

.el-submenu__title:hover:hover i {
  color: #fff !important;
}

.el-submenu .el-menu-item {
  min-width: 0 !important;
}

</style>